package cn.itrip.auth.service;
import cn.itrip.dao.ItripUserMapper;
import cn.itrip.pojo.ItripUser;
import cn.itrip.utlis.MD5Utils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
 * Created by LENOVO on 2018/8/17.
 */
@Service
public class RegisterServiceImpl implements RegisterService {
    @Resource
    private ItripUserMapper itripUserMapper;
    @Resource
    private ThirdPartyService thirdPartyService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 手机注册
     * @param itripUser
     */
    @Override
    public void registerUserByPhone(ItripUser itripUser) {
        itripUserMapper.insertSelective(itripUser);
        String VerificationCode= MD5Utils.createRandom();
        thirdPartyService.sendSms(itripUser.getUsercode(),VerificationCode);
        stringRedisTemplate.opsForValue().set(itripUser.getUsercode()+"的验证码", VerificationCode,60, TimeUnit.SECONDS);
    }

    /**
     * 短信验证
     * @param phone
     * @param code
     * @return
     */
    @Override
    public boolean validateSms(String phone,String code) {
        boolean result=false;
        if(stringRedisTemplate.hasKey(phone+"的验证码"))
        {
            String verificationCode=stringRedisTemplate.opsForValue().get(phone+"的验证码");

            if(verificationCode.equals(code))
            {
                ItripUser itripUser=itripUserMapper.selectByUserCode(phone);
                if(null!=itripUser)
                {
                    itripUser.setActivated(1);
                    itripUser.setUsertype(0);
                    itripUser.setFlatid(itripUser.getId());
                    itripUserMapper.updateByPrimaryKeySelective(itripUser);
                    result=true;
                }
                else
                {
                    result=false;
                }
            }
            else
            {
                result=false;
            }
        }
        else
        {
            result=false;
        }
       return result;
    }

    /**
     * 邮箱注册
     * @param itripUser
     * @throws Exception
     */
    @Override
    public void registerUserByMail(ItripUser itripUser) throws Exception {
        itripUserMapper.insertSelective(itripUser);
        String activationCode = MD5Utils.getMD5(new Date().toLocaleString(), 32);
        thirdPartyService.sendMail1(itripUser.getUsercode(),activationCode);
        stringRedisTemplate.opsForValue().set(itripUser.getUsercode()+"的激活码", activationCode,60*2, TimeUnit.SECONDS);
    }

    /**
     * 邮箱激活
     * @param mail
     * @param code
     * @return
     */
    @Override
    public boolean activateMail(String mail,String code) {
        boolean result=false;
        if(stringRedisTemplate.hasKey(mail+"的激活码"))
        {
            String activationCode=stringRedisTemplate.opsForValue().get(mail+"的激活码");

            if(activationCode.equals(code))
            {
                ItripUser itripUser=itripUserMapper.selectByUserCode(mail);
                if(null!=itripUser)
                {
                    itripUser.setActivated(1);
                    itripUser.setUsertype(0);
                    itripUser.setFlatid(itripUser.getId());
                    itripUserMapper.updateByPrimaryKeySelective(itripUser);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    /**
     * 查看用户是否存在
     * @param userCode
     * @return
     */
    @Override
    public boolean findUserByUserCode(String userCode)
    {
        boolean result=false;
        ItripUser itripUser=itripUserMapper.selectByUserCode(userCode);
        if(null!=itripUser) {
            result = true;
        }
        return  result;
    }
}
